
/**
 * @file
 * This file contains the javascript functions used to display a map when the
 * entity it is attached to is displayed.
 */

/**
 * Declare global variable by which to identify the map.
 */
var google_map_field_map;
var google_map_id;

/**
 * Add code to generate the map on page load.
 */
(function ($) {
  Drupal.behaviors.google_map_field = {
    attach: function (context) {
      // Pick up all elements of class google_map_field and loop
      // through them calling the google_map_field_load_map function
      // with the object ID.
      $(".google_map_field_display").each(function(index, item) {
        var objId = $(item).attr('id');
        google_map_id = objId;
        google_map_field_load_map(objId);
      });
    }
  };
})(jQuery);

/**
 * This function is called by the google_map_field Drupal.behaviour and
 * loads a google map in tot he given map ID container.
 */
function google_map_field_load_map(map_id) {
  
  /* cor do mapa */  
  var styles = [
  {
    stylers: [
      { hue: "#FF690B" },
      { saturation: 50 }
    ]
  },{
    featureType: "road",
    elementType: "geometry",
    stylers: [
      { lightness: 100 },
      { visibility: "simplified" }
    ]
  },{
    featureType: "road",
    elementType: "labels",
    stylers: [
      { visibility: "off" }
    ]
  }
  ];
  
  /* imagem do pin */
  var imagem = Drupal.settings.basePath + "sites/all/themes/qgdi/images/PIN.png";
  //var imagem = "http://www4.qgdi2014.com.br/sites/all/themes/qgdi/images/PIN.png";
  
  //console.log(imagem);
    
  // Get the settings for the map from the Drupal.settings object.
  var lat = Drupal.settings.gmf_node_display[map_id]['lat'];
  var lon = Drupal.settings.gmf_node_display[map_id]['lon'];
  var zoom = parseInt(Drupal.settings.gmf_node_display[map_id]['zoom']);
  // Create the map coords and map options.
  var latlng = new google.maps.LatLng(lat, lon);
  var mapOptions = {
    zoom: zoom,
    center: latlng,
    streetViewControl: false,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  // create the map.
  google_map_field_map = new google.maps.Map(document.getElementById(map_id), mapOptions);
  // Drop a marker at the specified position.
  marker = new google.maps.Marker({
    position: latlng,
    optimized: false,
    icon: imagem,
    map: google_map_field_map
  });
  
  google_map_field_map.setOptions({styles: styles});
  
}

/* Editado por Bruno 
 * Calcula a rota
 * */
var directionsService;
var directionsDisplay = new google.maps.DirectionsRenderer();
    
function calcRoute(chegada, origem, tipo) {
    
    /* endereços */
    var start = origem;
    var end = chegada;
    
    /* padrao carro */
    var mode = google.maps.DirectionsTravelMode.DRIVING;
    
    /* tipo igual a transporte publico */
    if (tipo == "TRANSIT") {
       mode = google.maps.DirectionsTravelMode.TRANSIT; 
    }
    
    var request = {
        origin:start, 
        destination:end,
        travelMode: mode
    };
    
    /* setando o mapa */
    directionsDisplay.setMap(google_map_field_map);
    directionsService = new google.maps.DirectionsService();
    
    /* traçando rota */
    directionsService.route(request, function(response, status) {
    
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
        } else {
            //if (status == "NOT_FOUND") {
                alert("Rota não encontrada...");
            //}
        }
 
    });
}
/* fim */

/* Editado Por Bruno */
/* Encontrar o Mapa pelo Endereço */
var geocoder = new google.maps.Geocoder();

function codeAddress(address) {
    
    /* imagem do pin */
    var imagem = Drupal.settings.basePath + "sites/all/themes/qgdi/images/PIN.png";
    
    //directionsDisplay.setMap(google_map_field_map);
    geocoder = new google.maps.Geocoder();
    
    geocoder.geocode({
        'address': address
        
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            google_map_field_map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: google_map_field_map,
                icon: imagem,
                position: results[0].geometry.location
            });
        } else {
            alert('Endereço não encontrado...' + status);
        }
    });
}
/* fim */